<?php
namespace zake\foundation\reactor;

use zake\foundation\able\reactor\LoopAble;
use zake\foundation\able\reactor\LoopAdaptAble;
use zake\foundation\able\stream\ReadAble;
use zake\foundation\able\stream\RwBufferAble;
use zake\foundation\able\stream\WriteAble;

/**
 * Class Loop
 * @package zake\foundation\reactor
 *
 *
 */
class Reactor extends BaseLoop implements LoopAble
{
    /**
     * @var BaseLoop|null|Reactor|LoopAble
     */
    protected static $singleton = null;


    /**
     * 构造器
     * @param null|LoopAdaptAble $adapt
     * @return BaseLoop|null|Reactor|LoopAble
     * @author liu.bin 2020-04-03 23:16
     */
    public static function singleton($adapt=null)
    {
        if(is_null(static::$singleton)){
            static::$singleton = new static($adapt);
        }
        return static::$singleton;
    }


    /**
     * 添加侦听处理
     *
     * @author liu.bin 2020-03-25 13:45
     *
     * @param ReadAble $stream
     * @param null $callback
     */
    public static function addReadListener(ReadAble $stream,\Closure $handler)
    {
        self::singleton()->addRead($stream,$handler);
    }



    /**
     * 移除侦听
     *
     * @author liu.bin 2020-03-25 13:45
     */
    public static function removeReadListener(ReadAble $stream)
    {
        self::singleton()->removeRead($stream);
    }



    /**
     * 添加事件侦听处理
     *
     * @author liu.bin 2020-03-25 13:45
     */
    public static function addWriteListener(WriteAble $stream,\Closure $handler)
    {
        self::singleton()->addWrite($stream,$handler);
    }



    /**
     * 添加事件侦听处理
     *
     * @author liu.bin 2020-03-25 13:45
     */
    public static function removeWriteListener(WriteAble $stream)
    {
        self::singleton()->removeWrite($stream);
    }


    /**
     * 添加时间侦听
     * @param $interval
     * @param \Closure $handler
     * @return mixed
     *
     * @author liu.bin 2020-04-27 22:55
     */
    public static function addTimeListener($interval,\Closure $handler,$param=[])
    {
        return self::singleton()->addTime($interval,$handler,$param);
    }


    /**
     * 移除时间侦听
     * @param $timeId
     *
     * @author liu.bin 2020-04-27 22:56
     */
    public static function removeTimeListener($timeId)
    {
        self::singleton()->removeTime($timeId);
    }



    /**
     * 添加信号侦听
     * @param int $sigNo
     * @param \Closure $handler
     * @return mixed
     *
     * @author liu.bin 2020-04-27 22:55
     */
    public static function addSignalListener($sigNo,\Closure $handler)
    {
        return self::singleton()->addSignal($sigNo,$handler);
    }


    /**
     * 移除信号侦听
     * @param $timeId
     *
     * @author liu.bin 2020-04-27 22:56
     */
    public static function removeSignalListener($sigNo)
    {
        self::singleton()->removeSignal($sigNo);
    }


    /**
     * 获取当前连接
     * @param int $fd
     * @return RwBufferAble
     *
     * @author liu.bin 2020-04-30 17:25
     */
    public static function getConnect($fd)
    {
        return self::singleton()->getReadStream($fd);
    }


    /**
     * 获取所有当前的读监听
     * @return array
     *
     * @author liu.bin 2020-05-07 13:16
     */
    public static function getConnectMap()
    {
        return self::singleton()->getReadStreamMap();
    }

}